package cn.exlu.coco.core.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static final String PTN_YYYYMMDD = "yyyyMMdd";
	public static final String PTN_YYYYMMDD_HHMMSS = "yyyyMMddHHmmss";
	public static final String PTN_YYYYMMDD_HHMM = "yyyyMMddHHmm";
	public static final String PTN_YYYYMMDD_HHMM_DAH = "yyyy-MM-dd HH:mm";
    
    /**
     * 接口格式yyyyMMddHHmmss
     */
    private static final SimpleDateFormat sdfInf=new SimpleDateFormat("yyyyMMddHHmmss");
    
    /**
     * 数据库格式yyyy-MM-dd HH:mm:ss
     */
    private static final SimpleDateFormat sdfDateDb = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 检索日期格式yyyy-MM-dd
     */
    private static final SimpleDateFormat sdfDateSearch = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 业务日期时间格式yyyy-MM-dd HH:mm:ss
     */
    private static final SimpleDateFormat sdfDateTimeBus = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    /**
     * 业务日期格式yyyy-MM-dd
     */
    private static final SimpleDateFormat sdfDateBus = new SimpleDateFormat("yyyy-MM-dd");
    
    /**
     * 唯一临时长日期yyyyMMddHHmmssSSS
     */
    private static final SimpleDateFormat sdfDateTimestamp = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    
    public static final Timestamp currentDbTimestamp(){
    	return new Timestamp(System.currentTimeMillis());
    }
    
    public static final Date currentBusDate(){
        return new Date();
    }
    
    public static final Timestamp currentBusTimeStamp(){
        return new Timestamp(new Date().getTime());
    }
    
    public static final String currentBusDateValue(){
        return sdfDateBus.format(new Date());
    }
    
    public static final String currentBusDateTime(){
    	return sdfDateTimeBus.format(new Date());
    }
    
    public static final String currentDateTimestamp(){
        return sdfDateTimestamp.format(new Date());
    }
    
    public static final long currentTimeMillis(){
        return System.currentTimeMillis();
    }


    public static final Date parseDateTimeBus(String date){
        try {
            return sdfDateTimeBus.parse(date);
        } catch (ParseException e) {
//            e.printStackTrace();
            return null;
        }
    }

    public static final String formatDateTimeBus(Date date){
        if(date == null){
            return null;
        }
        return sdfDateTimeBus.format(date);
    }

    
    public static final Date parseDateBus(String date){
        try {
            return sdfDateBus.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }
    
    public static final Date parseDate(String date, String ptn){
    	SimpleDateFormat sdf = new SimpleDateFormat(ptn);
    	try {
            return sdf.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }
    
    public static final String formatDateBus(Date date){
        if(date == null){
            return null;
        }
        return sdfDateBus.format(date);
    }

    /**
     * 2018-05-03之后不建议使用
     * @param date
     * @return
     */
    @Deprecated
    public static final Date parseDateInf(String date){
        try {
            return sdfInf.parse(date);
        } catch (ParseException e) {
           return null;
        }
    }

    /**
     * 2018-05-03之后不建议使用
     * @param date
     * @return
     */
    @Deprecated
    public static final String formatDateInf(Date date){
        return sdfInf.format(date);
    }
    
    public static final Date parseDateDb(String date){
        try {
            return sdfDateDb.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }
    
    public static final String formatDateDb(Date date){
        return sdfDateDb.format(date);
    }


    public static final String formatDateByPtn(Date date, String ptn){
        SimpleDateFormat sdf=new SimpleDateFormat(ptn);
        return sdf.format(date);
    }

    /**
     * 获取当前日期年月日
     * @return
     */
    public static final Date currentSearchDate(){
        Date nowDate = new Date();
        try {
            return sdfDateSearch.parse(sdfDateSearch.format(nowDate));
        } catch (ParseException e) {
            return null;
        }
        
    }
    
    /**
     * 计算查询日期的开始日期
     * @param dateFrom 格式：yyyy-MM-dd
     * @return
     */
    public static Date calcDateSearchFrom(String dateFrom){
        try {
            return sdfDateSearch.parse(dateFrom);
        } catch (ParseException e) {
            
//            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 计算查询日期的开始日期
     * @param dateFrom 格式：yyyy-MM-dd
     * @return
     */
    public static Date calcDateSearchFrom(Date date) {
    	try {
            return sdfDateSearch.parse(sdfDateSearch.format(date));
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 计算查询日期的结束日期
     * @param dateTo 格式：yyyy-MM-dd
     * @return
     */
    public static Date calcDateSearchTo(String dateTo){
        return addDays(dateTo, 1);
    }

    public static Date calcDateSearchFromByYear(String yearFrom){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        try {
            return sdf.parse(yearFrom);
        } catch (ParseException e) {
            return null;
        }
    }
    
    public static Date calcDateSearchFromByYearMonth(String yearMonthFrom){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        try {
            return sdf.parse(yearMonthFrom);
        } catch (ParseException e) {
            return null;
        }
    }
    
    public static void main(String[] args) {
//    	Date date = DateUtil.parseDate("20180131", PTN_YYYYMMDD);
//    	Date lastDate = lastDateMonth(date);
    	System.out.println(DateUtil.parseDateTimeBus("201811260350"));
    }
    
    /**
     * 计算两个日期相隔的小时
     * @param firstDate
     * @param secondDate
     * @return
     */
    public static int calcHoursBetweenTwoDate(Date firstDate, Date secondDate) {
        int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (60 * 60 * 1000));
        return nDay;
    }

    /**
     * 计算指定日期与当前日期相差天数，当天相差天数为0.
     * 大于0：当前日期离指定日期的天数
     * 小于0：当前日期已过指定日期天数
     * @param standardDate
     * @return
     */
    public static int calcDayByNow(String standardDate){
        String nowDate = sdfDateBus.format(new Date());
        return calcDaysBetweenTwoDate(nowDate, standardDate);
    }
    
    public static int calcDayByNow(Date date){
        return calcDaysBetweenTwoDate(new Date(), date);
    }
    
    // 计算两个日期相隔的天数
    public static int calcDaysBetweenTwoDate(Date firstDate, Date secondDate) {
        int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
        return nDay;
    }
    
    /**
     * 计算两个日期相隔的分钟
     * @param firstDate
     * @param secondDate
     * @return
     */
    public static int calcMinutesBetweenTwoDate(Date firstDate, Date secondDate){
        int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (60 * 1000));
        return nDay;
    }

    /**
     * 计算两个日期相隔的分钟
     * @param firstDate
     * @param secondDate
     * @return
     */
    public static int calcMinutesBetweenTwoDate(String firstDate, String secondDate){

        int nDay;
        try {
            nDay = (int) ((sdfDateTimeBus.parse(secondDate).getTime() - sdfDateTimeBus.parse(firstDate).getTime()) / (60 * 1000));
        } catch (ParseException e) {
            return 0;
        }

        return nDay;
    }
    
    // 计算两个日期相隔的秒数
    public static int calcSecondsBetweenTwoDate(Date firstDate, Date secondDate){
        return (int)((secondDate.getTime() - firstDate.getTime()) / 1000);
    }
    
    // 计算两个日期相隔的天数
    public static int calcDaysBetweenTwoDate(String firstString, String secondString) {

        Date firstDate = null;
        Date secondDate = null;
        try {
            firstDate = sdfDateBus.parse(firstString);
            secondDate = sdfDateBus.parse(secondString);
        } catch (Exception e) {
            // 日期型字符串格式错误
            return -999999;
        }

        int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
        return nDay;
    }
    
    /**
     * 
     * @param date yyyy-MM-dd
     * @param n
     * @return
     */
    public static Date addYears(String date, int n){
        return addYears(calcDateSearchFrom(date), n);
    }
    
    public static Date addYears(Date date, int n){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, n);
        return c.getTime();
    }
    
    /**
     * 
     * @param date yyyy-MM-dd
     * @param n
     * @return
     */
    public static Date addMonths(String date, int n){
        return addMonths(calcDateSearchFrom(date), n);
    }
    
    public static Date addMonths(Date date, int n){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, n);
        return c.getTime();
    }
    
    public static Date addDays(String date, int n){
        return addDays(calcDateSearchFrom(date), n);
    }

    public static Date addDays(Date date, int n){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, n);
        return c.getTime();
    }

    public static Date addHours(Date date, int n){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR, n);
        return c.getTime();
    }
    
    public static Date addMinutes(Date date, int n){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, n);
        return c.getTime();
    }
    
    public static Date addSeconds(Date date, int n){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.SECOND, n);
        return c.getTime();
    }
    
    public static Date addMilliseconds(Date date, int n){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MILLISECOND, n);
        return c.getTime();
    }
    
    public static boolean isEqualsByYear(Date d1, Date d2){
        String d1_year =  formatDateByPtn(d1, "yyyy");
        String d2_year =  formatDateByPtn(d2, "yyyy");
        
        return d1_year.equals(d2_year);
    }
    
    /**
     * 获取指定日期的最后一天
     * @param date
     * @return
     */
    public static Date lastDateMonth(Date date){
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(date);
    	calendar.set(Calendar.DAY_OF_MONTH, 1);
    	calendar.add(Calendar.MONTH, 1);
    	calendar.add(Calendar.DAY_OF_MONTH, -1);
    	return calendar.getTime();
    }
    
    /**
     * 获取指定日期的第一天
     * @param date
     * @return
     */
    public static Date firstDateMonth(Date date) {
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(date);
    	calendar.set(Calendar.DAY_OF_MONTH, 1);
    	
    	return calendar.getTime();
    }
   
}
